package top.lyooo.mybatislite.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author :LiYu
 * @date :2020-08-26 22:42
 * @desc :
 * @return :
 */
public class MyBatisUtils {
    public static SqlSessionFactory factory = null;

    public static SqlSession getConn(String classpath){
        if (factory == null) {
            InputStream reader = null;
            try {
                reader = Resources.getResourceAsStream(classpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            factory = new SqlSessionFactoryBuilder().build(reader);
        }
        return factory.openSession();
    }
    public static SqlSession getConn(){
        return factory.openSession();
    }
    public static void close(SqlSession sqlSession){
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}
